package com.foogui.boot.mybatisplus;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

/**
 * MP代码生成器
 *
 * @author Foogui
 * @date 2023/07/30
 */
public class CodeGenerator {
    /**
     * 项目路径
     */
    private static final String PROJECT = "D:/Gen";

    /**
     * 根路径
     */
    private static final String ROOT ="com.qm.ep.rebate";

    /**
     * 资源路径
     */
    private static final String RESOURCES = "D:/Gen";

    private static final Map<OutputFile, String> pathInfo= new HashMap<>();

    private static final Map<String, String> dbInfoMap = new HashMap<>();


    static {
        pathInfo.put(OutputFile.mapperXml, RESOURCES);
        dbInfoMap.put("url", "127.0.0.1");
        dbInfoMap.put("port", "3308");
        dbInfoMap.put("db", "ep_rebate");
        dbInfoMap.put("username", "root");
        dbInfoMap.put("password", "root");
    }

    public static void main(String[] args) {
        String jdbcPath = String.format("jdbc:mysql://%s:%s/%s?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true", dbInfoMap.get("url"), dbInfoMap.get("port"), dbInfoMap.get("db"));
        FastAutoGenerator.create(jdbcPath, dbInfoMap.get("username"), dbInfoMap.get("password"))
                .globalConfig(builder -> {
                    builder.author("FooGui") // 设置作者
                            // .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(PROJECT); // 指定输出目录
                })

                .packageConfig(builder -> {

                    builder.parent(ROOT)
                            // .moduleName("test")  // 设置所在模块名
                            .controller("controller")
                            .entity("domain.bean")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .pathInfo(pathInfo);// 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder
                            // .likeTable(new LikeTable("rebate_extract", SqlLike.RIGHT))
                            .addInclude("userinfo_data")
                            // .addInclude("common_businessdata")
                            // .addTablePrefix("XXX_")//过滤掉的文件名
                            .entityBuilder()
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .formatFileName("%sPO")
                            .enableLombok()                  // 开启Lombok，会在实体类前添加 [@Getter] 和 [@Setter] 注解
//                            .logicDeleteColumnName("delete_flag")   // 逻辑删除字段名
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper")// resources文件中xxMapper.xml 文件后缀

                            .enableBaseColumnList()   // 会在mapper.xml文件生成[通用查询结果列 ]配置
                            .enableBaseResultMap(); //会在mapper.xml文件生成[通用查询映射结果]配置。
                })

                .templateEngine(new FreemarkerTemplateEngine())// 使用Freemarker
                .execute();
    }
}
